package com.liao.controller;

import com.liao.bean.Module;
import com.liao.bean.Order;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @RequestMapping("/queryOrderListByPage")
    public PageResult<Order> queryOrderListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                                  @RequestParam(defaultValue = "5") Integer pageSize,
                                                  @RequestParam(defaultValue = "")  String state,
                                                  @RequestParam(defaultValue = "") String orderId) throws Exception {
        PageResult<Order> pageResult = orderService.queryModuleListByPage(currentPage,pageSize,state,orderId);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("/addOrder")
    public Result addOrder(String orderId,String buyerPhone,String orderTime,String state,String userId,String address) throws Exception {
        orderService.addOrder(orderId,buyerPhone,orderTime,state,userId,address);
        return new Result(true, Code.ORDER_ADD_SUCCESS, Msg.ORDER_ADD_SUCCESS);
    }


    @ResponseBody
    @RequestMapping("/deleteOrder")
    public Result deleteOrder(String orderId) throws Exception {
        orderService.deleteOrder(orderId);
        return new Result(true,Code.ORDER_DELETE_SUCCESS, Msg.ORDER_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteOrderIds")
    public Result deleteOrderIds(@RequestParam("orderIds[]") List<String> orderIds) throws Exception {
        for (String orderId : orderIds) {
            try {
                orderService.deleteOrder(orderId);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.ORDER_DELETE_FAIL, Msg.ORDER_DELETE_FAIL);
            }

        }
        return new Result(true,Code.ORDER_DELETE_SUCCESS, Msg.ORDER_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/editOrder")
    public Result editOrder(@RequestBody Order order) throws Exception {
        orderService.editOrder(order.getOldOrderId(),order.getOrderId(),order.getBuyerPhone(),order.getOrderTime(),order.getState().toString(),
                order.getUserId(),order.getAddress());
        return new Result(true,Code.ORDER_EDIT_SUCCESS, Msg.ORDER_EDIT_SUCCESS);
    }
}
